---
title: "The Discovery of Information in the Information Age"
author: | 
  | Jenna Christensen and Michael O'Hara, PhD
  | Department of Economics
  | St. Lawrence University
  | 23 Romoda Drive 
  | Canton, New York 13617
output:
  pdf_document: default
  word_document: default
bibliography: NSA-Prism-Sources.bib
keywords: Information, Technology, Privacy Disclosure
abstract: |-
  In this appendix to the paper, we do all the same calculations for the event study, but use the Nasdaq100 index as the market index rathern than the S&P 500. 
---



```{r, message=FALSE, warning=FALSE, include=FALSE}
rm(list = ls())

library(pdfetch)
library(tidyverse)
library(xts)
library(reshape2)
library(plyr)
library(knitr)
library(readr)
library(kableExtra)
library(stargazer)

```


```{r, message=FALSE, warning=FALSE, include=FALSE}

##  Set dates for estimation window and event window
Est.Data.Start <- "2012-05-18"
Est.Data.End <- "2013-03-31"

Event.Data.Start <- "2013-05-28"
Event.Data.End <- "2013-07-01"

##  Set dates to break up event period into windows

Leak.Date <- as.Date("2013-06-05")
Protest.Date <- as.Date("2013-06-14")
Complaint.Date <- as.Date("2013-06-22")

## Set index to be used in market model
## as a string, eh?

market.index <- "Nasdaq"

##  To import raw data directly (soup-to-nuts), run line below
##  source("Supplementary_Code/Fetch_data.R")
```


```{r, message=FALSE, warning=FALSE, include=FALSE}

###  Import data if not starting from scratch
Est.Data <- read.csv("../ImportableData/Import_Estimation_Snowden", row.names = 1)
Event.Data <- read.csv("../ImportableData/Import_Event_Snowden", row.names = 1)

```

```{r, message=FALSE, warning=FALSE, include=FALSE}
###  Create returns dataframes

     ## Estimation window
log.Est.Data <- apply(Est.Data, 2, log)
Est.Returns <- apply(log.Est.Data, 2, diff)
Est.Returns <- as.data.frame(Est.Returns)

Est.Dates <- as.Date(row.names.data.frame(Est.Returns)) 

rm(log.Est.Data)

     ## Event window
log.Event.Data <- apply(Event.Data, 2, log)
Event.Returns <- apply(log.Event.Data, 2, diff)
Event.Returns <- as.data.frame(Event.Returns)

Event.Dates <- as.Date(row.names.data.frame(Event.Returns)) 

rm(log.Event.Data)

```

```{r}
## Plot of stock prices during event period

Event.plot.dat <- data.frame(Date = as.Date(row.names(Event.Data)), coredata(Event.Data))
Event.plot.dat <- Event.plot.dat %>%
  ## Standardize prices so that they can be on same graph
  mutate( SP500= SP500/SP500[1], Comcast = Comcast/Comcast[1], Charter = Charter/Charter[1], ATT = ATT/ATT[1], Verizon = Verizon/Verizon[1], Century = Century/Century[1], Google = Google/Google[1], Microsoft = Microsoft/Microsoft[1], Apple = Apple/Apple[1], Facebook = Facebook/Facebook[1], Yahoo = Yahoo/Yahoo[1], AOL = AOL/AOL[1]) %>%
  gather(key = "Company", value = "Price", SP500, Comcast, Charter, ATT, Verizon, Century, Google, Microsoft, Apple, Facebook, Yahoo, AOL)

ggplot(Event.plot.dat, aes(x = Date)) + 
  geom_line(aes(y = Price, color = Company)) +
  
  geom_vline(xintercept = Leak.Date) +
  annotate("text", x = Leak.Date, y = 1.07, label = "Leaks Begin", size = 2.75) +
  
  geom_vline(xintercept = Protest.Date) + 
  annotate("text", x = Protest.Date, y = 1.07, label = "Protest", size = 2.75) + 
  
  geom_vline(xintercept = Complaint.Date) +
  annotate("text", x = Complaint.Date, y = 1.07, label = "Criminal Complaint Filed", size = 2.75)

```

```{r, echo=FALSE}
###   Compute summary stats of returns
###   in estimation and event windows

     ###   Estimation window
Est.Returns.Mean <- apply(Est.Returns, 2, mean)
Est.Returns.sd <- apply(Est.Returns, 2, sd) 

EstimationWindowSS <- as.data.frame(cbind(Est.Returns.Mean, Est.Returns.sd))
rm(Est.Returns.Mean, Est.Returns.sd)
names(EstimationWindowSS) <- c("Avg Returns", "SD Returns")


     ###   Event window
Event.Returns.Mean <- apply(Event.Returns, 2, mean)
Event.Returns.sd <- apply(Event.Returns, 2, sd) 

EventWindowSS <- as.data.frame(cbind(Event.Returns.Mean, Event.Returns.sd))
rm(Event.Returns.Mean, Event.Returns.sd)
names(EventWindowSS) <- c("Avg Returns", "SD Returns")

    ##  Make tables of summary stats
kable(EstimationWindowSS, digits = 3, booktabs = T, caption = "Snowden Event Study: Estimation Window Summary Statistics")

kable(EventWindowSS, digits = 3, booktabs = T, caption = "Snowden Event Study: Event Window Summary Statistics")
```


```{r echo=FALSE, message=FALSE, warning=FALSE}
###  Parameter estimation

reg.index <- eval(parse(text = paste("Est.Returns$", market.index, sep = "")))
   ###  Define function to do regressions
es.reg <- function(y){
  reg.model <- lm(y ~ reg.index)
  est.summary <- summary(reg.model)$coefficients[2,]
}

reg.dat <- Est.Returns %>%
  select(-SP500, -Nasdaq)

betas <- t(apply(reg.dat, 2, es.reg))

## Create a table of betas and significance stats
names(betas) <- c("Beta", "Std Error", "T Stat", "P-value")
kable(betas, digits = 3, booktabs = T, caption = "Beta Estmates")

```


```{r}
###  Check why Facebook is fucked up

FB.plot.dat <- data.frame(Date = as.Date(row.names(Est.Returns)), coredata(Est.Returns))
FB.plot.dat <- FB.plot.dat %>%
  select(Date, Nasdaq, Facebook) %>%
  gather(key = "Company", value = "Return", Nasdaq, Facebook)

ggplot(FB.plot.dat, aes(x = Date)) + 
  geom_line(aes(y = Return, color = Company)) 

## it looks like FB is significantly more volatile than the S&P

FB.plot.dat <- data.frame(Date = as.Date(row.names(Est.Data)), coredata(Est.Data))
FB.plot.dat <- FB.plot.dat %>%
  mutate( SP500= SP500/SP500[1], Nasdaq = Nasdaq/Nasdaq[1], Comcast = Comcast/Comcast[1], Charter = Charter/Charter[1], Att = Att/Att[1], Verizon = Verizon/Verizon[1], Century = Century/Century[1], Google = Google/Google[1], Microsoft = Microsoft/Microsoft[1], Apple = Apple/Apple[1], Facebook = Facebook/Facebook[1], Yahoo = Yahoo/Yahoo[1], AOL = AOL/AOL[1]) %>%
  select(Date, Nasdaq, Facebook) %>%
  gather(key = "Company", value = "Price", Nasdaq, Facebook)

ggplot(FB.plot.dat, aes(x = Date)) + 
  geom_line(aes(y = Price, color = Company)) 
```



```{r echo=FALSE, message=FALSE, warning=FALSE}
###  Computation of abnormal returns
reg.index <- eval(parse(text = paste("Est.Returns$", market.index, sep = "")))
nr.index <- eval(parse(text = paste("Event.Returns$", market.index, sep = "")))
#reg.index <- Est.Returns$SP500
#nr.index <- Event.Returns$SP500

   ###  Define function to compute normal returns
norm.returns <- function(y){
  reg.model <- lm(y ~ reg.index)
  betas <- reg.model$coefficients
  
  nr <- betas[1] + betas[2] * nr.index
}

reg.dat <- Est.Returns %>%
  select(-SP500, -Nasdaq)

   ## This returns a matrix of normal returns calculated
   ## for each company
   ## It will be the same dimension as the Event.Returns data
   ## minus the indexes
normal.returns <- as.data.frame(apply(reg.dat, 2, norm.returns))
Abret.Combined <- select(Event.Returns, -SP500, -Nasdaq) - normal.returns


```



```{r echo=FALSE, message=FALSE, warning=FALSE}
###  Computation of t-stats for abnormal returns

reg.index <- eval(parse(text = paste("Est.Returns$", market.index, sep = "")))
nr.index <- eval(parse(text = paste("Event.Returns$", market.index, sep = "")))

   ###  Define function 
sd.returns <- function(y){
  reg.model <- lm(y ~ reg.index)
  resids <- reg.model$residuals
  resid.var <- (sum(resids^2))/(nrow(Est.Returns) - 2)
  resid.sd  <- sqrt(resid.var)
  resid.sd  <- rep(resid.sd, nrow(Event.Returns))
}

#reg.dat <- Est.Returns %>%
  #select(-SP500, -Nasdaq)

   ## This returns a matrix of normal returns calculated
   ## for each company
   ## It will be the same dimension as the Event.Returns data
   ## minus the indexes
sdev.returns <- as.data.frame(apply(reg.dat, 2, sd.returns))
TStats <- Abret.Combined/ sdev.returns

```




```{r, message=FALSE, warning=FALSE, include=FALSE}

###  MO: This chunk is apprently to add days of the week to the abnormal returns matrix
###  but there are 24 days listed and there are only 23 days in the event data
###  once returns are computed

#Weekday <- c("Tuesday", "Wednesday", "Thursday", "Friday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday")

```


```{r, message=FALSE, warning=FALSE, echo=FALSE}

## GRAPH T-STATS

#####################################
##########    GRAPH T-STATS FROM ABNORMAL RETURNS

###  Clean up the data a bit for plotting
tstat.plot <- data.frame(Date = Event.Dates, TStats)
tstat.plot <- tstat.plot %>%
  ## Put data into form that ggplot likes
  gather(key = "Company", value = "TStat", Comcast, Charter, ATT, Verizon, Century, Google, Microsoft, Apple, Facebook, Yahoo, AOL) %>%
  mutate(Company = as.factor(Company))

###   Make fancy plot of abnormal returns
###   I have done it here by periods
ggplot(tstat.plot, aes(x = Date)) + 
  geom_line(mapping = aes(y = TStat, color = Company)) +
     ##  Significance lines
  geom_hline(yintercept = 2, color = "red") + 
  geom_hline(yintercept = -2, color = "red") +
  geom_hline(yintercept = 1.65, color = "green") +
  geom_hline(yintercept = -1.65, color = "green") +
     ##  Important Dates
  geom_vline(xintercept = Leak.Date) +
  annotate("text", x = Leak.Date, y = -3, label = "Leaks Begin", size = 2.75) +
  geom_vline(xintercept = Protest.Date) + 
  annotate("text", x = Protest.Date, y = -3, label = "Protest", size = 2.75) + 
  geom_vline(xintercept = Complaint.Date) +
  annotate("text", x = Complaint.Date, y = -3, label = "Criminal Complaint Filed", size = 2.75) + 
      ## Window text
  annotate("text", x = Event.Dates[9], y = 4, label = "Reveal Period", size = 3.5) +
  annotate("text", x = Event.Dates[16], y = 4.15, label = "Protest", size = 3.5) +
  annotate("text", x = Event.Dates[16], y = 3.85, label = "Period", size = 3.5) +
  annotate("text", x = Event.Dates[21], y = 4, label = "Charge Period", size = 3.5) +
  ylab("Abnormal returns T-stats")
  
```

** MO: edited to here **

**WORKS TO HERE**








```{r}

### MO: Looks like this chunk just makes a bunch of separate tables
### For the different periods.
### Not sure why

Day <- c("Leak -1", "Leak", "Leak +1", "Leak +2", "Leak +3", "Leak +4", "Leak +5",
         "Protest -1", "Protest +1", "Protest +2", "Protest +3", "Protest +4",
         "Complaint -1", "Complaint +1", "Complaint +2","Complaint +3", "Complaint +4", "Complaint +5") 

#Dataframes of abnormal returns during periods
TStats.leak <- TStats %>%
  mutate(Date = as.Date(row.names(TStats))) %>%
  filter(Date >= Leak.Date) %>%
  mutate(Day = Day) 
  #select(Date, Day, everything())


########## Providers

Provider.TStat.Table <- TStats.leak %>%
  select(Date, Day, Comcast, Charter, ATT, Verizon, Century)

 
kable(Provider.TStat.Table, digits = 2, booktabs = T, caption = "T Statistics for daily abnormal returns - Providers")

########## Sites

Site.TStat.Table <- TStats.leak %>%
  select(Date, Day, Google, Microsoft, Apple, Facebook, AOL, Yahoo)


kable(Site.TStat.Table, digits = 2, booktabs = T, caption = "T Statistics for daily abnormal returns - Sites")
```



```{r, message=FALSE, warning=FALSE, include=FALSE}
###  CUMULATIVE ABNORMAL RETURNS 
## Calculate cumulative abnormal returns by adding abnormal returns over the event window
## Variance of CAR is (length of event window + 1) * se

Abret.Combined <- Abret.Combined %>%
  mutate(Date = Event.Dates)

### CARS for whole event period
Event.period <- Abret.Combined %>%
  filter(Date > Leak.Date) %>%
  select(-Date)

CAR.event <- data.frame(CAR = apply(Event.period,2,sum), vCAR = apply(Event.period,2,var))
CAR.event <- CAR.event %>%
  mutate(TCAR = CAR/sqrt(vCAR * (nrow(Event.period) + 1)))
rm(Event.period)


#CARs for Leak Window

Leak.period <- Abret.Combined %>%
  filter(Date > Leak.Date & Date < (Protest.Date -1)) %>%
  select(-Date)

CAR.leak <- data.frame(CAR = apply(Leak.period,2,sum), 
                       vCAR = apply(Leak.period,2,var))
CAR.leak <- CAR.leak %>%
  mutate(TCAR = CAR/sqrt(vCAR * (nrow(Leak.period) + 1)))
rm(Leak.period)


#CARs for Protest Window: 

Protest.period <- Abret.Combined %>%
  filter(Date >= (Protest.Date - 1) & Date < (Complaint.Date -1)) %>%
  select(-Date)

CAR.protest <- data.frame(CAR = apply(Protest.period,2,sum), 
                       vCAR = apply(Protest.period,2,var))
CAR.protest <- CAR.protest %>%
  mutate(TCAR = CAR/sqrt(vCAR * (nrow(Protest.period) + 1)))
rm(Protest.period)



#CARs for Complaint Period

Complaint.period <- Abret.Combined %>%
  filter(Date >= (Complaint.Date -1)) %>%
  select(-Date)

CAR.complaint <- data.frame(CAR = apply(Complaint.period,2,sum), 
                       vCAR = apply(Complaint.period,2,var))
CAR.complaint <- CAR.complaint %>%
  mutate(TCAR = CAR/sqrt(vCAR * (nrow(Complaint.period) + 1)))
rm(Complaint.period)





#Dataframe of CARs during all event windows 

Event.CARs <- data.frame(row.names(CAR.leak), CAR.leak$CAR, CAR.protest$CAR, CAR.complaint$CAR, CAR.event$CAR)
names(Event.CARs) <- c("Company", "Leak", "Protest", "Complaint", "Event")


#Dataframe of TCARs during all event windows

Event.TCARs <- data.frame(row.names(CAR.leak), CAR.leak$TCAR, CAR.protest$TCAR, CAR.complaint$TCAR, CAR.event$TCAR)
names(Event.TCARs) <- c("Company", "Leak", "Protest", "Complaint", "Event")


```




** CHECKED UP TO HERE **




$\pagebreak$

References {#references .unnumbered}
==========